import 'package:flutter/material.dart';

/// 按钮 500 毫秒内 不能 多次点击
class IntervalButton extends StatefulWidget {
  const IntervalButton({
    Key key,
    @required this.child,
    @required this.onPressed,
    this.onHighlightChanged,
    this.textTheme,
    this.textColor,
    this.disabledTextColor,
    this.color,
    this.disabledColor,
    this.focusColor,
    this.hoverColor,
    this.highlightColor,
    this.splashColor,
    this.colorBrightness,
    this.padding,
    this.visualDensity,
    this.shape,
    this.focusNode,
    this.materialTapTargetSize,
    this.milliseconds = 500,//时间 间隔 默认 500 毫秒
  }) : super(key: key);

  final Widget child;
  final VoidCallback onPressed;
  final ValueChanged<bool> onHighlightChanged;
  final ButtonTextTheme textTheme;
  final Color textColor;
  final Color disabledTextColor;
  final Color color;
  final Color disabledColor;
  final Color focusColor;
  final Color hoverColor;
  final Color highlightColor;
  final Color splashColor;
  final Brightness colorBrightness;
  final EdgeInsetsGeometry padding;
  final VisualDensity visualDensity;
  final ShapeBorder shape;
  final Clip clipBehavior = Clip.none;
  final FocusNode focusNode;
  final bool autofocus = false;
  final MaterialTapTargetSize materialTapTargetSize;
  final int milliseconds;
  @override
  _IntervalButtonState createState() => _IntervalButtonState();
}

class _IntervalButtonState extends State<IntervalButton> {
  bool _isCan;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _isCan = true;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: FlatButton(
        onPressed: () => _buttonClick(),
        child: widget.child,
        textTheme: widget.textTheme,
        textColor: widget.textColor,
        disabledTextColor: widget.disabledTextColor,
        color: widget.color,
        disabledColor: widget.disabledColor,
        focusColor: widget.focusColor,
        hoverColor: widget.hoverColor,
        highlightColor: widget.highlightColor,
        splashColor: widget.splashColor,
        colorBrightness: widget.colorBrightness,
        padding: widget.padding,
        visualDensity: widget.visualDensity,
        shape: widget.shape,
        clipBehavior: widget.clipBehavior,
        focusNode: widget.focusNode,
        autofocus: widget.autofocus,
        materialTapTargetSize: widget.materialTapTargetSize,
      ),
    );
  }
  /// 按钮点击事件
  _buttonClick() {
    if (widget.onPressed != null && _isCan) {
      widget.onPressed();
      _isCan = false;
      // 500 毫秒内 不能多次点击
      Future.delayed(Duration(milliseconds: widget.milliseconds), () {
        _isCan = true;
      });
    }
  }
}
